package com.easywan.inteceptor;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.easywan.pojo.IResult;
import com.easywan.pojo.IResultSupport;
import com.easywan.service.IToolsCounterService;
import com.easywan.util.Utils;
import com.easywan.utils.ToolNamesUtils;
import com.easywan.vo.BaseUsers;

import junit.framework.Assert;
import net.sf.json.JSONObject;

@Component
public class LoginInteceptor implements HandlerInterceptor{
	
	
	Logger logger = Logger.getLogger(LoginInteceptor.class);
	
	public void afterCompletion(HttpServletRequest request, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
			throws Exception {
	
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		
		BaseUsers users = (BaseUsers) request.getSession().getAttribute("users");
		logger.info("用户访问:"+users);
		if(users == null){
			IResult result= IResultSupport.buildResult(IResult.FAILT_CODE, "用户未登录");
			response.getWriter().write(JSONObject.fromObject(result).toString());
			return false;
		}
		
		return true;
	}

	
	
}
